import matplotlib.pyplot as plt
import numpy as np

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DV Sans']
plt.rcParams['axes.unicode_minus'] = False

def motion_problem():
    # 模拟物体运动函数
    def s(t):
        # 假设运动不是匀速的
        return 100 * (t/100)**1.5  # s(0)=0, s(100)=100
    
    t = np.linspace(0, 100, 1000)
    positions = s(t)
    
    # 定义辅助函数 f(t) = s(t+20) - s(t) - 20
    t_inner = np.linspace(0, 80, 800)  # t ∈ [0,80] 保证 t+20 ∈ [20,100]
    f_t = s(t_inner + 20) - s(t_inner) - 20
    
    plt.figure(figsize=(12, 8))
    
    plt.subplot(2, 1, 1)
    plt.plot(t, positions, 'b-', linewidth=2, label='位移函数 s(t)')
    plt.xlabel('时间 t (秒)'); plt.ylabel('位移 s(t) (米)')
    plt.legend(); plt.grid(True, alpha=0.3)
    
    plt.subplot(2, 1, 2)
    plt.plot(t_inner, f_t, 'r-', linewidth=2, label='辅助函数 f(t) = s(t+20) - s(t) - 20')
    plt.axhline(y=0, color='k', linestyle='-', alpha=0.3)
    
    # 验证端点值
    f0 = s(20) - s(0) - 20
    f80 = s(100) - s(80) - 20
    plt.scatter([0, 80], [f0, f80], color='green', s=100)
    
    # 寻找零点
    root_idx = np.argmin(np.abs(f_t))
    t0 = t_inner[root_idx]
    plt.scatter([t0], [f_t[root_idx]], color='red', s=100, 
               label=f'零点 t_0 ≈ {t0:.1f}')
    
    plt.xlabel('时间 t (秒)'); plt.ylabel('f(t)')
    plt.legend(); plt.grid(True, alpha=0.3)
    
    plt.tight_layout()
    plt.show()
    
    print(f"f(0) = s(20) - s(0) - 20 = {f0:.2f}")
    print(f"f(80) = s(100) - s(80) - 20 = {f80:.2f}")
    print(f"存在 t_0 ≈ {t0:.1f} 使 f(t_0) = 0")
    print(f"验证: s({t0:.1f}+20) - s({t0:.1f}) = {s(t0+20) - s(t0):.2f} 米")

motion_problem()